package com.mls.web.controller.education;

import java.util.List;

import com.mls.common.utils.ShiroUtils;
import com.mls.framework.shiro.service.SysPasswordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mls.common.annotation.Log;
import com.mls.common.enums.BusinessType;
import com.mls.education.domain.EduUser;
import com.mls.education.service.IEduUserService;
import com.mls.common.core.controller.BaseController;
import com.mls.common.core.domain.AjaxResult;
import com.mls.common.utils.poi.ExcelUtil;
import com.mls.common.core.page.TableDataInfo;

/**
 * 用户信息Controller
 * 
 * @author
 * @date 2022-02-19
 */
@Controller
@RequestMapping("/education/user")
public class EduUserController extends BaseController
{
    private String prefix = "education/user";

    @Autowired
    private IEduUserService eduUserService;

    @Autowired
    private SysPasswordService passwordService;

    @RequiresPermissions("education:user:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    /**
     * 查询用户信息列表
     */
    @RequiresPermissions("education:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EduUser eduUser)
    {
        startPage();
        List<EduUser> list = eduUserService.selectEduUserList(eduUser);
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @RequiresPermissions("education:user:export")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EduUser eduUser)
    {
        List<EduUser> list = eduUserService.selectEduUserList(eduUser);
        ExcelUtil<EduUser> util = new ExcelUtil<EduUser>(EduUser.class);
        return util.exportExcel(list, "用户信息数据");
    }

    /**
     * 新增用户信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户信息
     */
    @RequiresPermissions("education:user:add")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EduUser eduUser)
    {
        //设置密码盐
        eduUser.setSalt(ShiroUtils.randomSalt());
        eduUser.setPassword(
                passwordService.encryptPassword(eduUser.getUsername(), eduUser.getPassword(), eduUser.getSalt())
        );
        return toAjax(eduUserService.insertEduUser(eduUser));
    }

    /**
     * 修改用户信息
     */
    @RequiresPermissions("education:user:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        EduUser eduUser = eduUserService.selectEduUserById(id);
        mmap.put("eduUser", eduUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户信息
     */
    @RequiresPermissions("education:user:edit")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EduUser eduUser)
    {
        return toAjax(eduUserService.updateEduUser(eduUser));
    }

    /**
     * 删除用户信息
     */
    @RequiresPermissions("education:user:remove")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(eduUserService.deleteEduUserByIds(ids));
    }
}
